package com.mq.dpca.file;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
import com.mq.dpca.msg.MQConfig;
import com.mq.dpca.util.ReadCmdLine;
import com.mq.dpca.util.RenameUtil;

/**
 * 
 * MQ��������ļ�����
 * ������ѯ
 */
public class MQFileReceiver {
	private MQQueueManager qmgr; // ���ӵ����й�����

	private MQQueue inQueue; // �������

	private String queueName = ""; // �������

	private String host = ""; //

	private int port = 1414; // �������Ķ˿ں�

	private String channel = ""; // ͨ�����

	private String qmgrName = ""; // ���й�����

	private MQMessage inMsg; // ������Ϣ����

	private MQGetMessageOptions gmo; // ���û�ȡ��Ϣѡ��

	private static String fileName = null; // ���ն����ϵ���Ϣ�������ļ�

	private int ccsid = 0;

	private static String file_dir = null;
	

	/**
	 * ��������
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		MQFileReceiver mfs = new MQFileReceiver();
		//��ʼ������
		mfs.initproperty();
		//�����ļ�
		mfs.runGoupReceiver();
		//��ȡshell�ű���
//		String shellname = MQConfig.getValueByKey(fileName);
//		if(shellname!=null&&!"".equals(shellname)){
//			//����shell
//			ReadCmdLine.callShell(shellname);
//		}else{
//			System.out.println("have no shell name,Only receive files.");
//		}

	}

	public void runGoupReceiver() {
		try {
			init();
			getGroupMessages();
			qmgr.commit();
			System.out.println("\n Messages successfully Receive ");
		} catch (MQException mqe) {
			mqe.printStackTrace();
			try {
				System.out.println("\n Backing out Transaction ");
				qmgr.backout();
				System.exit(2);
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(2);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(2);
		}
	}

	/**
	 * ��ʼ��������������Ϣ
	 * 
	 * @throws Exception
	 */
	private void init() throws Exception {
		/* Ϊ�ͻ�����������MQEnvironment���� */
		MQEnvironment.hostname = host;
		MQEnvironment.channel = channel;
		MQEnvironment.port = port;

		/* ���ӵ����й����� */
		qmgr = new MQQueueManager(qmgrName);

		/* ���ö��д�ѡ������ */
		int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF
				| MQConstants.MQOO_FAIL_IF_QUIESCING;

		/* �򿪶������� */
		inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
	}

	/**
	 * �����ļ���������
	 * 
	 * @throws Exception
	 */
	public void getGroupMessages() {
		/* ���û�ȡ��Ϣѡ�� */
		gmo = new MQGetMessageOptions();
		gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING;
		gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT;
		/* �ȴ���Ϣ */
		gmo.options = gmo.options + MQConstants.MQGMO_WAIT;
		/* ���õȴ�ʱ������ */
		gmo.waitInterval = 5000;
		/* ֻ��ȡ��Ϣ */
		gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE;
		/* �Լ�˳���ȡ��Ϣ */
		gmo.options = gmo.options + MQConstants.MQGMO_LOGICAL_ORDER;
		gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID;
		/* ������Ϣ���� */
		inMsg = new MQMessage();
		try {
			FileOutputStream fos = null;
			/* ��������Ϣ */
			while (true) {
				try {
					inQueue.get(inMsg, gmo);
					if (fos == null) {
						try {
							fileName = inMsg.getStringProperty("fileName");
							String fileName_full = null;
							fileName_full = file_dir + RenameUtil.rename(fileName);
							fos = new FileOutputStream(new File(fileName_full));
							int msgLength = inMsg.getMessageLength();
							byte[] buffer = new byte[msgLength];
							inMsg.readFully(buffer);
							fos.write(buffer, 0, msgLength);
							/* �鿴�Ƿ��������Ϣ��ʶ */
							char x = gmo.groupStatus;
							if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
								System.out.println("Last Msg in Group");
								break;
							}
							inMsg.clearMessage();

						} catch (Exception e) {
							System.out
									.println("Receiver the message without property,do nothing!");
							inMsg.clearMessage();
						}
					} else {
						int msgLength = inMsg.getMessageLength();
						byte[] buffer = new byte[msgLength];
						inMsg.readFully(buffer);
						fos.write(buffer, 0, msgLength);
						/* �鿴�Ƿ��������Ϣ��ʶ */
						char x = gmo.groupStatus;
						if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
							System.out.println("Last Msg in Group");
							break;
						}
						inMsg.clearMessage();
					}
				} catch (Exception e) {
					char x = gmo.groupStatus;
					if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
						System.out.println("Last Msg in Group");
					}
					break;
				}
			}
			if (fos != null)
				fos.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void initproperty() {
		MQConfig config = new MQConfig().getInstance();
		if (config.getMQ_MANAGER() != null) {
			qmgrName = config.getMQ_MANAGER();
			queueName = config.getMQ_QUEUE_NAME();
			channel = config.getMQ_CHANNEL();
			host = config.getMQ_HOST_NAME();
			port = Integer.valueOf(config.getMQ_PROT());
			ccsid = Integer.valueOf(config.getMQ_CCSID());
//			file_dir = config.getFILE_DIR();
		}
	}
	
	public void fileReciveDir(String dir){
		file_dir = dir;
		initproperty();
		runGoupReceiver();
	}
}
